﻿using System;
using System.IO;
using System.Net.Mail;

/// <summary>
/// Summary description for Mailer
/// </summary>
public class Mailer
{
    SmtpClient m_emailClient = null;
    MailMessage m_emailMessage = null;
    String m_emailPlainText = String.Empty;
    String m_emailHTMLBody = String.Empty;
    String m_embeddedImage = String.Empty; 
    
    public Mailer()
    {
        m_emailClient = new SmtpClient("latbsex01.ladomain.lintasarta.co.id");
        m_emailMessage = new MailMessage();
        m_emailMessage.From = new MailAddress("info@lintasarta.co.id", "Lintasarta Mailer");
    }

    public void AddRecipient(String sxEmailAddress)
    {
        m_emailMessage.To.Add(sxEmailAddress);
    }

    public void AddRecipient(params String[] sxEmailAddresses)
    {
        foreach (String sxEmailAddress in sxEmailAddresses)
            AddRecipient(sxEmailAddress);
    }

    public void AddAttachment(String sxAttachmentFilename)
    {
        m_emailMessage.Attachments.Add(new Attachment(sxAttachmentFilename));
    }

    public void AddAttachment(params String[] sxAttachmentFilenames)
    {
        foreach (String sxAttachmentFilename in sxAttachmentFilenames)
            AddAttachment(sxAttachmentFilename);
    }

    public void AddCC(String sxEmailAddress)
    {
        m_emailMessage.CC.Add(sxEmailAddress);
    }

    public void AddCC(params String[] sxEmailAddresses)
    {
        foreach (String sxEmailAddress in sxEmailAddresses)
            AddCC(sxEmailAddress);
    }

    public void AddBCC(String sxEmailAddress)
    {
        m_emailMessage.Bcc.Add(sxEmailAddress);
    }

    public void AddBCC(params String[] sxEmailAddresses)
    {
        foreach (String sxEmailAddress in sxEmailAddresses)
            AddBCC(sxEmailAddress);
    }

    public MailAddress EmailSender
    {
        get { return m_emailMessage.From; }
        set { m_emailMessage.From = value; }
    }

    public String EmailSubject
    {
        get { return m_emailMessage.Subject; }
        set { m_emailMessage.Subject = value; }
    }

    public String EmailPlainText
    {
        get { return m_emailPlainText; }
        set { m_emailPlainText = value; }
    }

    public String EmailHTMLBody
    {
        get { return m_emailHTMLBody; }
        set { m_emailHTMLBody = value; }
    }

    public String EmbeddedImage
    {
        get { return m_embeddedImage; }
        set { m_embeddedImage = value; }
    }

    public void SendMail()
    {
        String sxHTML = String.Empty;
        AlternateView htmlView;
        if (m_emailMessage.To.Count < 1)
            throw new Exception("Mail recipient is not specified!");

        //first we create the Plain Text part
        AlternateView plainView = AlternateView.CreateAlternateViewFromString(
            m_emailPlainText, null, "text/plain");
        //check if the embeddedImage is available or not
        if (!String.IsNullOrEmpty(m_embeddedImage))
        {
            FileInfo fi = new FileInfo(m_embeddedImage);
            if (!fi.Exists)
                throw new Exception("Embedded image file is not exist!");

            sxHTML = String.Format("<div>{0}</div><div><img src=cid:embedImage></div>", m_emailHTMLBody);
            //then we create the Html part
            //to embed images, we need to use the prefix 'cid' in the img src value
            //the cid value will map to the Content-Id of a Linked resource.
            //thus <img src='cid:companylogo'> will map to a LinkedResource with a ContentId of 'companylogo'
            htmlView = AlternateView.CreateAlternateViewFromString(
                sxHTML, null, "text/html");

            //create the LinkedResource (embedded image)
            LinkedResource logo = new LinkedResource(m_embeddedImage);
            logo.ContentId = "embedImage";
            //add the LinkedResource to the appropriate view
            htmlView.LinkedResources.Add(logo);
        }
        else
        {
            sxHTML = String.Format("<div>{0}</div>", m_emailHTMLBody);
            htmlView = AlternateView.CreateAlternateViewFromString(
                sxHTML, null, "text/html");
        }
        m_emailMessage.AlternateViews.Add(plainView);
        m_emailMessage.AlternateViews.Add(htmlView);

        try
        {
            m_emailClient.Send(m_emailMessage);
        }
        finally
        {
            foreach (Attachment x in m_emailMessage.Attachments)
            {
                if (x != null)
                    x.Dispose();
            }

            if (m_emailMessage != null)
            {
                m_emailMessage.Attachments.Clear();
                m_emailMessage.Dispose();
            }

            m_emailClient = null;
        }
    }
}
